﻿<?php


class ProtApp extends BaseApp
{

    var $type = 'prot';
	
    function index()
    {
        if(!isset($_POST['word']))
		{
		$_POST['word'] = '';
		}
        if(!isset($_GET['v']))
		{
		$_GET['v'] = '';
		}
	    $word = $_POST['word'];
		$word_array = explode(' ',$word);
		
		foreach($word_array as $key => $value)
		{
			if(!$value)
			{
             unset($word_array[$key]);
		    }  
		}
		 
        $order = '';
		$v = $_GET['v'];
		if($v == 'timeasc')
		{
        $order = 'addtime asc';
		}
		else if($v == 'timedesc')
		{
        $order = 'addtime desc';		
		}

				
	
	
	
		$TB_new = &$this->_model('prot');
				
		$data = $TB_new->show($order,$v,$word_array);	
			
        $this->assign('lw_data', $data['data']);		
		$this->assign('lw_page', $data['page']);	
		
		
	 	$categorydata = & $GLOBALS['SCup']->_get_category_data($this->type);
		
		$categorylist = array();
		if(empty($categorydata))$categorydata = array();
		foreach((array)$categorydata as $value)
		{
		    $categorylist[$value['cat_id']] = $value['cat_name'];
		}		

        $this->assign('categorylist', $categorylist);							
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('prot_list.view.html');
    }


    function kindeditor()
    {
    $w = intval($_GET['w']);
    include(ROOT_PATH."/data/ewebeditor/ewebeditor.lib.php");	
	$kindeditor = new Ewebeditor(array('imageUploadCgi'=>SITE_URL.'/admin/index.php?app='.$this->type.'&act=kindeditorupfile&w='.$w));
	$kindeditor->show();
	
    }
	
    function kindeditorupfile()
    {
    $w = intval($_GET['w']);
    include(ROOT_PATH."/data/ewebeditor/ewebeditor.lib.php");	
	$kindeditor = new Ewebeditor(array('imageUploadCgi'=>SITE_URL.'/admin/index.php?app='.$this->type.'&act=kindeditorupfile&w='.$w));
	$kindeditor->upfile($this->type.'/'.$w);	
    }
	
	

    function cagryadd()
    {
		$GLOBALS['Lang']->_get_lang('prot');	
	    $category = & $GLOBALS['SCup']->_get_category();
		 		 		
		$catgydata = $category->show($this->type);
		
		$catgystr = $category->html_format_category($catgydata,0);	

        $this->assign('catgystr', $catgystr);	
        $this->assign('appname', $this->type);
        $this->assign('modulename', '团购产品');			
        $this->assign('category_value', $this->type);
        $this->assign('viewdata', array());				    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('cagryadd.view.php');
    }
	
    function editcatgy()
    {
		$GLOBALS['Lang']->_get_lang('prot');	
		$id= intval($_GET['id']);
		
	    $category = & $GLOBALS['SCup']->_get_category();
		 		 		
		$catgydata = $category->show($this->type);
		
		$data = $category->oneshow($id);
		
		$catgystr = $category->html_format_category($catgydata,$data['parent_id'],0,0,$id);	

        $this->assign('data', $data);
        $this->assign('appname', $this->type);
        $this->assign('modulename', '团购产品');
        $this->assign('catgystr', $catgystr);
		
        $this->assign('category_value', $this->type);
        $this->assign('viewdata', array());				    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('cagryedit.view.php');
    }
	
    function cagrylist()
    {
		$GLOBALS['Lang']->_get_lang('prot');		
	    $category = & $GLOBALS['SCup']->_get_category();
		 		 		
		$catgydata = $category->show($this->type);
		$catgydata = $category->get_list_category($catgydata);
        $this->assign('appname', $this->type);
        $this->assign('modulename', '团购产品');
		
        $this->assign('catgydata', $catgydata);				

        $this->assign('category_value', $this->type);
	    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('cagrylist.view.php');
    }

    function cyaddpost()
    {

	     $data = $_POST['data'];
         
		 $category = & $GLOBALS['SCup']->_get_category();
		 		 		
		 $category->post($data);
		 
		 $category->write_cache($this->type);	
		 	 		 
		 $this->cagrylist();

    }


    function cyeditpost()
    {

	     $id = $_POST['id'];

	     $data = $_POST['data'];
         
		 $category = & $GLOBALS['SCup']->_get_category();
		 		 		
		 $category->editaction($id,$data);

		 $category->write_cache($this->type);	
		 	 		 
		 $this->cagrylist();

    }
	
    function delcatgy()
    {
		$id= intval($_GET['id']);		

	    $category = & $GLOBALS['SCup']->_get_category();		 		 		
		$category->del($id);
		
		$category->write_cache($this->type);
				      
		$this->cagrylist();   
    }	
	
    function upfile()
    {
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	 if(!isset($_GET["edit"]))
	 {
	 $_GET["edit"]='';
	 }	 
	 if(!isset($_GET["picurl"]))
	 {
	 $_GET["picurl"]='';
	 }	
	 if(!isset($_GET['jsfun']))
	 {
	 $_GET['jsfun']='';
	 }		 
	 if(!isset($_GET['putid']))
	 {
	 $_GET['putid']='';
	 }		 
	 if(!isset($_GET['uppic']))
	 {
	 $_GET['uppic']='';
	 }			
		$action = $_POST["action"];		
		$id = intval($_GET['id']);		
		$jsfun = $_GET['jsfun'];
		$uppic = $_GET['putid'];
		if(!empty($_GET['uppic']))
		{
		$uppic = $_GET['uppic'];		
		}	
		$edit = $_GET["edit"];
		$picurl = $_GET["picurl"];

        $this->assign('jsfun', $jsfun);		

        $this->assign('uppic', $uppic);		

        $this->assign('edit', $edit);		

        $this->assign('picurl', $picurl);


						
		if($action == 'post')
		{
			 if(!isset($_POST['jsfun']))
	 {
	 $_POST['jsfun']='';
	 }	
	 if(!isset($_POST['uppic']))
	 {
	 $_POST['uppic']='';
	 }		 
	 if(!isset($_POST["edit"]))
	 {
	 $_POST["edit"]='';
	 }		 
	 if(!isset($_POST["picurl"]))
	 {
	 $_POST["picurl"]='';
	 }	
	 if(!isset($_POST['id']))
	 {
	 $_POST['id']='';
	 }			
		       $jsfun = $_POST['jsfun'];
		       $uppic = $_POST['uppic'];		
		       $edit = $_POST["edit"];
		       $picurl = $_POST["picurl"];
		 	   $id || $id = intval($_POST['id']);		
		       $filedname = trim(str_replace(array("UP_"),array(""),$uppic)); 
       
		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile($this->type.'/'.$id); 	  
	           $updata = $upfile->_batch_upfile($this->type);		 
		 		 
		       $newfile = $updata[1]['path'];
	       
			   if($edit=='editp' && $picurl)
			   {

			          $TB_new = &$this->_model('content');			    		 		 		
		              $TB_new->editpicurl($id,array("$filedname"=>$newfile),$this->table_id);
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}			   
			   }

			    if($edit=='edit' && $picurl)
		       {
		              $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}			 
		       }
		
		      $pic_url = SITE_URL . '/' . UPFILE_PATH . '/' .$newfile;

	    echo "<img src='{$pic_url}' id='upsrc' width='100px' height='100px'><br/><A HREF='?app=".$this->type."&act=upfile&edit=$edit&picurl=$newfile&id=$id&jsfun=$jsfun&uppic=$uppic'>修改图片</A>";

	    echo "<script>
				if(self==top){
					window.opener.$jsfun('$newfile','$uppic');
					window.self.close();
				}else{
					window.parent.$jsfun('$newfile','$uppic');
				}
		     </script>";
				
	    exit;		
		
		}

        $this->assign('id', $id);
	    $this->assign('app', $this->type);		    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('upfile.view.php');
    }




    function pupfile()
    {	
	 if(!isset($_GET["action"]))
	 {
	 $_GET["action"]='';
	 }
	 if(!isset($_POST["action"]))
	 {
	 $_POST["action"]='';
	 }
	 if(!isset($_GET['id']))
	 {
	 $_GET['id']='';
	 }
	        if(!isset($_POST['id']))
	        {
	          $_POST['id']='';
	        } 	
		$action = $_GET["action"];			
		$action || $action = $_POST["action"];
		
		$id = intval($_GET['id']);
		$cid =$id;
				
		if($action == 'post')
		{
		
		 	   $id || $id = intval($_POST['id']);		
               $cid = $id;
		       include(ROOT_PATH.'/lib/upfile.lib.php');	  
	           $upfile = new Upfile($this->type.'/'.$id); 	  
	           $updata = $upfile->_batch_upfile($this->type);		 
		 		 
		       $newfile = $updata[1]['path'];
			   
               unset($data);
		       $data['uid'] = $GLOBALS['SCup']->SCup_user_msg['user_id'];		       
               $data['con_id'] = $id;
               $data['appname'] = $this->type;			   
			   $data['file_url'] = $updata[1]['path'];
			   $data['size'] = $updata[1]['size'];
			   $data['type'] = $updata[1]['type'];
			   			   
			   $TB_new = &$this->_model('upfile');			    		 		 		
		       $TB_new->post($data);
					   	   
	    }
		else if($action == 'del')
		{
			   $id || $id = intval($_POST['id']);   
			   $TB_new = &$this->_model('upfile');
			   
			    $picdata = $TB_new->oneshow($id);
				$picurl = $picdata['file_url'];
                $cid = $picdata['con_id'];
			   			    		 		 		
		       $TB_new->del($id);
					  
		       $pic_dir= ROOT_PATH . '/' . UPFILE_PATH . '/' . $picurl;
		if(!is_dir($pic_dir)&&file_exists($pic_dir))
		{
		    del_file($pic_dir);	
		}			
		
		}			

	    $TB_new = &$this->_model('upfile');

        $this->assign('picdata', $TB_new ->selct($cid,$this->type));		
	
        $this->assign('id', $cid);
	    $this->assign('app', $this->type);					    
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', SITE_TITLE);
        $this->assign('page_description', SITE_DESCRIPTION);
        $this->assign('page_keywords', SITE_KEYWORDS);	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('pupfile.view.php');
    }



    function edit()
    {	
		$id= intval($_GET['id']);
		
		$TB_new = &$this->_model('prot');
				
		$data = $TB_new->oneshow($id);		

		$TB_group = &$this->_model('group');
				
		$groupdata = $TB_group->oneshow($data['groupid']);
       $this->assign('groupname', $groupdata['name']);		

	 	$categorydata = & $GLOBALS['SCup']->_get_category_data($this->type);
		
        $this->assign('categorydata', $categorydata);

        $this->assign('data', $data);

        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');		
		
        $this->display('prot_edit.view.html');   
    }

    function add()
    {	
	
	
		$TB_new = &$this->_model('prot'); 
		
		$id_array = $TB_new->select_id();		
		foreach($id_array as $value)
		{
		  if($value['id']&&file_exists(ROOT_PATH . '/' . UPFILE_PATH . '/' . $this->type . '/' . $value['id'] . '/'))
		  {
		    del_file(ROOT_PATH . '/' . UPFILE_PATH . '/' . $this->type . '/' . $value['id'] . '/');	
		  }		
		}		
		$TB_new->delet_id();	
		
				
		$data_id = $TB_new->insert_id();

		$data = array();
		$data['id'] = $data_id;
        $this->assign('data', $data);		
        $this->assign('id', $data_id);	
				
		
	 	$categorydata = & $GLOBALS['SCup']->_get_category_data($this->type);
		   
        $this->assign('categorydata', $categorydata);	
		
        $this->assign('view_charset', VIEW_CHARSET);	
        $this->assign('page_title', $GLOBALS['Lang']->_get_lang_var(SITE_TITLE));
        $this->assign('page_description', $GLOBALS['Lang']->_get_lang_var(SITE_DESCRIPTION));
        $this->assign('page_keywords', $GLOBALS['Lang']->_get_lang_var(SITE_KEYWORDS));	
		
        $this->assign('site_url',SITE_URL);
		
        $this->assign('web_view_dir',SITE_URL.'/admin/view/default/');
		
        $this->display('prot_add.view.html');
    }


    function addpost()
    {

	     $data = $_POST['data'];
		 $id = $_POST['id'];
		 if(!isset($data['display']))
		 {
		   $data['display']='';
		 }	 		 
		 $data['user_id'] = $GLOBALS['SCup']->SCup_user_msg['user_id'];
		 $data['user_name'] = $GLOBALS['SCup']->SCup_user_msg['data']['user_name'];	 
		 if(!isset($data['titlecolor']))
		 {
		   $data['titlecolor']='';
		 }
		 if(!isset($data['fonttype']))
		 {
		   $data['fonttype']='';
		 } 
		 $data['config'] = serialize(array('titlecolor'=>$data['titlecolor'],'fonttype'=>$data['fonttype'])); 
		 unset($data['titlecolor'],$data['fonttype']); 
		$data['groupid'] =  str_replace("-", "",$data['groupid']);

 		$TB_group = &$this->_model('group');  
        $tempgroup = $TB_group->oneshow($data['groupid']);
        $data['sellerid'] = $tempgroup['sellerid'];		          

		 $TB_new = &$this->_model('prot'); 
		 		 		
		 $TB_new->post($id,$data);
		 		 
		 $this->index();
    }


    function editpost()
    {

		$id= intval($_POST['id']);
		
		$data = $_POST['data'];
		 if(!isset($data['display']))
		 {
		   $data['display']='';
		 }	
		 if(!isset($data['titlecolor']))
		 {
		   $data['titlecolor']='';
		 }
		 if(!isset($data['fonttype']))
		 {
		   $data['fonttype']='';
		 } 
		 $data['config'] = serialize(array('titlecolor'=>$data['titlecolor'],'fonttype'=>$data['fonttype'])); 
		 unset($data['titlecolor'],$data['fonttype']); 
		 		 
		$data['groupid'] =  str_replace("-", "",$data['groupid']);	
 		$TB_group = &$this->_model('group');  
        $tempgroup = $TB_group->oneshow($data['groupid']);
        $data['sellerid'] = $tempgroup['sellerid'];

 /*		 $data['user_id'] = $GLOBALS['SCup']->SCup_user_msg['user_id'];
		 $data['user_name'] = $GLOBALS['SCup']->SCup_user_msg['data']['user_name'];	 
 */
 		$TB_new = &$this->_model('prot');  
				
		$TB_new->editaction($id,$data);
			
		$this->index();  
		 

    }


    function del()
    {

		$id= intval($_GET['id']);	
		  if($id&&file_exists(ROOT_PATH . '/' . UPFILE_PATH . '/' . $this->type . '/' . $id. '/'))
		  {
		    del_file(ROOT_PATH . '/' . UPFILE_PATH . '/' . $this->type . '/' . $id. '/');	
		  }					
			
		$TB_new = &$this->_model('prot');
		
		$TB_new->del($id);
				      
        $this->index();   

    }

    function doajax()
    {

		$type = $_GET['type'];
		$ckfid = $_GET['ckfid'];
		$fuid = $_GET['fuid'];
		$inputid = $_GET['inputid'];
		$fid = $_GET['fid'];
        $db = &get_db();
		$pre = DB_PREFIX;

        header('Content-Type: text/html; charset='.VIEW_CHARSET);
        error_reporting(7); 
       if($type==1)
       {

		      $showThisId="{$fuid}_".rands(5);
		      echo "<select type=\"select\" name=\"data[groupid]\"  ><option value=\"\" style='color:blue;'>请选择</option>";
		      $query = $db->query("SELECT * FROM {$pre}group  WHERE cat_id='$fid'");
		      while($rs = $db->fetch_array($query))
			  {
			     $_r=$db->get_one("SELECT * FROM {$pre}group  WHERE id='$rs[id]'");
			     if($_r)
				 {
				     $rs[id]=-$rs[id];
			     }
			     echo " <option value=\"$rs[id]\">$rs[name]</option>";
		      }
		      echo "</select>";	

       }
       elseif($type==2)
       {	
	        if($ckfid)
			{
	 	          $fdivid="{$fuid}_".rands(5);
		          echo $shows=$this->get__sonfid($ckfid,$fdivid,0,"{$pre}category");
	        }
	        if(!$shows)
			{
		          $showThisId="{$fuid}_".rands(5);
		          echo "<select name=\"select\" onchange=\"showfid_S('$showThisId',this,'$fuid','$inputid','$type');\"><option value=\"\" style='color:blue;'>请选择</option>";
		          $query = $db->query("SELECT * FROM {$pre}category  WHERE parent_id='$fid' and type='group' ");
		          while($rs = $db->fetch_array($query))
				  {
			          $_r=$db->get_one("SELECT * FROM {$pre}category  WHERE parent_id='$rs[cat_id]'  and type='group' ");
			         if($_r)
					 {
				       $rs[id]=-$rs[id];
			         }
			      echo " <option value=\"$rs[cat_id]\">$rs[cat_name]</option>";
		          }
		          echo "</select>";
		          echo "<span id=\"$showThisId\"  style='display:none;'></span>";	
	       }
       }

   }



   function get__sonfid($ckfid,$divid,$num,$table)
   {
		$fuid= $_GET['fuid'];
		$inputid= $_GET['inputid'];
		$type= $_GET['type'];

        $db = &get_db();
		$pre = DB_PREFIX;
	$rsdb = $db->get_one("SELECT parent_id FROM $table WHERE cat_id='$ckfid'");
	$rsdb[fup]=intval($rsdb[fup]);
	$fdivid="{$fuid}_".rands(5);
	$show.= "<span id=\"$fdivid\" divname='$fuid'><select name=\"select\" onchange=\"showfid_S('$divid',this,'$fuid','$inputid','$type')\"><option value=\"\" style='color:blue;'>请选择</option>";
	$query = $db->query("SELECT * FROM $table WHERE parent_id='$rsdb[parent_id]'");
	while($rs = $db->fetch_array($query)){
		$_r=$db->get_one("SELECT * FROM $table WHERE parent_id='$rs[cat_id]'");
		$_c=$ckfid==$rs[id]?" style='color:red;' selected ":'';
		if($_r){
			$rs[id]=-$rs[id];
		}
		
		$show.= " <option value=\"$rs[cat_id]\" $_c>$rs[cat_name]</option>";
	}
	$show.= "</select></span>";

	if($rsdb[parent_id]||$num)
	{
		$num++;
		if(!$rsdb[parent_id])
		{
			$num=0;
		}
		$shows=$this->get__sonfid($rsdb[parent_id],$fdivid,$num,$table).$show;
	}
	return $shows;
    }

  
}

function rands($length,$strtolower=1) {
	$hash = '';
	$chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $length; $i++) {
		$hash .= $chars[mt_rand(0, $max)];
	}
	if($strtolower==1){
		$hash=strtolower($hash);
	}
	return $hash;
}


?>